/*
 * Copyright 2020 www.mytijian.com All right reserved. This software is the
 * confidential and proprietary information of www.mytijian.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with www.mytijian.com.
 */
package com.mytijian.task.httprequest;

import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class HttpRequest {
    private  RestTemplate restTemplate = RestTemplateBuilder.buildRestTemplate();
    private String serverPath;
    public HttpRequest(String serverPath) {
        this.serverPath = serverPath;
        if (this.serverPath == null) {
            this.serverPath = "";
        }
    }
    public HttpRequest(String serverPath, Integer readTimeout, Integer connectinTimeout) {
        this(serverPath);
        ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
        if (factory instanceof SimpleClientHttpRequestFactory) {
            SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = (SimpleClientHttpRequestFactory) factory;
            simpleClientHttpRequestFactory.setConnectTimeout(connectinTimeout);
            simpleClientHttpRequestFactory.setReadTimeout(readTimeout);
        }
    }
    public ResponseEntity getForRequest(String ipAndPort, String operate, Map<String, Object> param) {
        String url = getUrl(ipAndPort, operate, param);
        return restTemplate.getForEntity(url, String.class);
    }
    private String getUrl(String ipAndport, String operate, Map<String, Object> params) {
        String url = getUrl(ipAndport, operate);
        if (CollectionUtils.isEmpty(params)) {
            return url;
        }
        StringBuilder builder = new StringBuilder();
        builder.append("?");
        params.forEach((key, value)->{
            builder.append(key).append("=").append(value).append("&");
        });
        ;
        return url + builder.substring(0, builder.length() - 1);
    }
    private  String getUrl(String ipAndPort, String operate) {
        StringBuilder builder = new StringBuilder();
        builder.append("http://").append(ipAndPort);
        if (serverPath.indexOf("/") != 0) {
            builder.append("/").append(serverPath);
        }else {
            builder.append(serverPath);
        }
        if (operate.indexOf("/") != 0) {
            builder.append("/").append(operate);
        }else {
            builder.append(operate);
        }
        return builder.toString();
    }
    public  ResponseEntity postFoRequest(String ipAndPort, String operate, Object body) {
       return restTemplate.postForEntity(getUrl(ipAndPort, operate), body, String.class);
    }

}
